﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace MyGeneric20180617
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("2018 06 17");

                var list = new List<int>() { 1, 2, 3, 4 }.Select(i => new
                {
                    id = i,
                    name = "Test" + i
                });

                foreach (var item in list)
                {
                    Console.WriteLine(item.id);
                    Console.WriteLine(item.name);
                }

                int iValue = 123;
                string sValue = "456";
                DateTime dtValue = DateTime.Now;
                object oValue = new object();

                Console.WriteLine(typeof(List<int>));
                Console.WriteLine(typeof(Dictionary<string,int>));

                Console.WriteLine("****************************************");
                CommonMethod.ShowInt(iValue);
                CommonMethod.ShowString(sValue);
                CommonMethod.ShowDatetime(dtValue);

                Console.WriteLine("****************************************");
                CommonMethod.ShowObject(oValue);

                CommonMethod.ShowObject(iValue);
                CommonMethod.ShowObject(sValue);
                CommonMethod.ShowObject(dtValue);

                Console.WriteLine("****************************************");
                GenericMethod.Show<object>(oValue);
                GenericMethod.Show(iValue);         //类型参数可以省略，由编译器推断出来
                GenericMethod.Show<int>(iValue);    //类型参数必须和参数匹配
                GenericMethod.Show<string>(sValue);
                GenericMethod.Show<DateTime>(dtValue);

                Console.WriteLine("****************************************");
                //test base 用法， 在父类和子类的关系中
                Child child = new Child();

                Console.WriteLine("****************************************");
                People people =  new People()
                {
                    Id=11,
                    Name ="test"
                };

                Japenese jp = new Japenese()
                {
                    Id = 12,
                    Name = "test"
                };

                Chinese chinese = new Chinese()
                {
                    Id = 13,
                    Name = "test"
                };

                Hubei hb = new Hubei()
                {
                    Id = 14,
                    Name = "test"
                };

                //Contraint.Show<People>(people);
                Contraint.Show<Chinese>(chinese);
                Contraint.Show<Hubei>(hb);


                Contraint.ShowPeople(people);
                Contraint.ShowPeople(chinese);
                Contraint.ShowPeople(hb);

                //Contraint.Show<Japenese>(jp);   //虽然Japanese有ID和Name,但不是People子类，所以不能调用

                //Contraint.ShowInterface<People>(people);  //没有实现ISport借口，所以不能调用
                Contraint.ShowInterface<Chinese>(chinese);
                Contraint.ShowInterface<Hubei>(hb);

                Contraint.ShowInterface<Japenese>(jp);

                #region Monitor
                {
                    long commonTime = 0;
                    long objectTime = 0;
                    long genericTime = 0;

                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();

                        for (int i = 0; i < 1000000; i++)
                        {
                            ShowCommon(iValue);
                        }

                        sw.Stop();
                        commonTime = sw.ElapsedMilliseconds;
                    }

                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();

                        for (int i = 0; i < 1000000; i++)
                        {
                            ShowObject(oValue);
                        }

                        sw.Stop();
                        objectTime = sw.ElapsedMilliseconds;
                    }

                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();
                        for (int i = 0; i < 1000000; i++)
                        {
                            ShowGeneric<int>(iValue);
                        }

                        sw.Stop();
                        genericTime = sw.ElapsedMilliseconds;
                    }

                    Console.WriteLine("CommonTime={0} objectTime={1} genericTime={2}",commonTime, objectTime, genericTime);

                }
                #endregion

            }
            catch (Exception ex)
            {

            }
            Console.Read();
        }

        private static void ShowCommon(int iParameter)
        {

        }

        private static void ShowObject(object oParameter)
        {

        }

        private static void ShowGeneric<T>(T tParameter)
        {

        }
    }
}
